package com.pokeoffice.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.SwingConstants;

import com.pokeoffice.builder.CommandBuilder;
import com.pokeoffice.client.Client;
import com.pokeoffice.client.Session;
import com.pokeoffice.interfaces.Command;
import com.pokeoffice.users.UserType;

public class CreateUser extends JPanel {
	private final ButtonGroup buttonGroup = new ButtonGroup();

	private JTextField city;
	private JTextField region;
	private JTextField insurance;
	private JTextField pharmacy;
	private JTextField name;

	private Session session;
	private JTextField phone;

	public CreateUser(final Session session) {
		this.session = session;
		SpringLayout springLayout = new SpringLayout();
		setLayout(springLayout);

		final JComboBox typeBox = new JComboBox();
		springLayout.putConstraint(SpringLayout.NORTH, typeBox, 130,
				SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.WEST, typeBox, 400,
				SpringLayout.WEST, this);
		typeBox.setModel(new DefaultComboBoxModel(UserType.values()));
		add(typeBox);

		JLabel lblUserType = new JLabel("User Type");
		lblUserType.setForeground(Color.WHITE);
		springLayout.putConstraint(SpringLayout.NORTH, lblUserType, 0,
				SpringLayout.NORTH, typeBox);
		springLayout.putConstraint(SpringLayout.EAST, lblUserType, -36,
				SpringLayout.WEST, typeBox);
		add(lblUserType);

		JLabel lblName = new JLabel("Name");
		lblName.setForeground(Color.WHITE);
		springLayout.putConstraint(SpringLayout.NORTH, lblName, 30,
				SpringLayout.SOUTH, lblUserType);
		springLayout.putConstraint(SpringLayout.WEST, lblName, 0,
				SpringLayout.WEST, lblUserType);
		add(lblName);

		JLabel lblGender = new JLabel("Gender");
		lblGender.setForeground(Color.WHITE);
		springLayout.putConstraint(SpringLayout.NORTH, lblGender, 30,
				SpringLayout.SOUTH, lblName);
		springLayout.putConstraint(SpringLayout.WEST, lblGender, 0,
				SpringLayout.WEST, lblUserType);
		add(lblGender);

		final JRadioButton rdbtnM = new JRadioButton("M");
		rdbtnM.setForeground(Color.DARK_GRAY);
		buttonGroup.add(rdbtnM);
		springLayout.putConstraint(SpringLayout.NORTH, rdbtnM, 0,
				SpringLayout.NORTH, lblGender);
		springLayout.putConstraint(SpringLayout.WEST, rdbtnM, 0,
				SpringLayout.WEST, typeBox);
		add(rdbtnM);

		final JRadioButton rdbtnF = new JRadioButton("F");
		rdbtnF.setForeground(Color.DARK_GRAY);
		springLayout.putConstraint(SpringLayout.NORTH, rdbtnF, 0,
				SpringLayout.NORTH, rdbtnM);
		springLayout.putConstraint(SpringLayout.WEST, rdbtnF, 10,
				SpringLayout.EAST, rdbtnM);
		buttonGroup.add(rdbtnF);
		add(rdbtnF);

		city = new JTextField();
		springLayout.putConstraint(SpringLayout.WEST, city, 0,
				SpringLayout.WEST, typeBox);
		add(city);
		city.setColumns(10);

		JLabel lblCity = new JLabel("City");
		lblCity.setForeground(Color.WHITE);
		springLayout.putConstraint(SpringLayout.NORTH, city, -2,
				SpringLayout.NORTH, lblCity);
		springLayout.putConstraint(SpringLayout.NORTH, lblCity, 30,
				SpringLayout.SOUTH, lblGender);
		springLayout.putConstraint(SpringLayout.WEST, lblCity, 0,
				SpringLayout.WEST, lblUserType);
		add(lblCity);

		region = new JTextField();
		springLayout.putConstraint(SpringLayout.WEST, region, 0,
				SpringLayout.WEST, typeBox);
		add(region);
		region.setColumns(10);

		JLabel lblRegion = new JLabel("Region");
		lblRegion.setForeground(Color.WHITE);
		springLayout.putConstraint(SpringLayout.NORTH, region, -2,
				SpringLayout.NORTH, lblRegion);
		springLayout.putConstraint(SpringLayout.NORTH, lblRegion, 30,
				SpringLayout.SOUTH, lblCity);
		springLayout.putConstraint(SpringLayout.WEST, lblRegion, 0,
				SpringLayout.WEST, lblUserType);
		add(lblRegion);

		JLabel lblBirthdate = new JLabel("Birthdate");
		lblBirthdate.setForeground(Color.WHITE);
		springLayout.putConstraint(SpringLayout.NORTH, lblBirthdate, 30,
				SpringLayout.SOUTH, lblRegion);
		springLayout.putConstraint(SpringLayout.WEST, lblBirthdate, 0,
				SpringLayout.WEST, lblUserType);
		add(lblBirthdate);

		final JComboBox comboBox_1 = new JComboBox();
		comboBox_1.setModel(new DefaultComboBoxModel(
				new String[] { "1930", "1931", "1932", "1933", "1934", "1935",
						"1936", "1937", "1938", "1939", "1940", "1941", "1942",
						"1943", "1944", "1945", "1946", "1947", "1948", "1949",
						"1950", "1951", "1952", "1953", "1954", "1955", "1956",
						"1957", "1958", "1959", "1960", "1961", "1962", "1963",
						"1964", "1965", "1966", "1967", "1968", "1969", "1970",
						"1971", "1972", "1973", "1974", "1975", "1976", "1977",
						"1978", "1979", "1980", "1981", "1982", "1983", "1984",
						"1985", "1986", "1987", "1988", "1989", "1990", "1991",
						"1992", "1993", "1994", "1995", "1996", "1997", "1998",
						"1999", "2000", "2001", "2002", "2003", "2004", "2005",
						"2006", "2007", "2008", "2009", "2010" }));
		springLayout.putConstraint(SpringLayout.NORTH, comboBox_1, 0,
				SpringLayout.NORTH, lblBirthdate);
		springLayout.putConstraint(SpringLayout.WEST, comboBox_1, 0,
				SpringLayout.WEST, typeBox);
		add(comboBox_1);

		final JComboBox comboBox_2 = new JComboBox();
		springLayout.putConstraint(SpringLayout.WEST, comboBox_2, 10,
				SpringLayout.EAST, comboBox_1);
		comboBox_2.setModel(new DefaultComboBoxModel(new String[] { "01", "02",
				"03", "04", "05", "06", "07", "08", "09", "10", "11", "12" }));
		springLayout.putConstraint(SpringLayout.NORTH, comboBox_2, 0,
				SpringLayout.NORTH, lblBirthdate);
		add(comboBox_2);

		final JComboBox comboBox_3 = new JComboBox();
		springLayout.putConstraint(SpringLayout.WEST, comboBox_3, 10,
				SpringLayout.EAST, comboBox_2);
		comboBox_3.setModel(new DefaultComboBoxModel(new String[] { "01", "02",
				"03", "04", "05", "06", "07", "08", "09", "10", "11", "12",
				"13", "14", "15", "16", "17", "18", "19", "20", "21", "22",
				"23", "24", "25", "26", "27", "28", "29", "30", "31" }));
		springLayout.putConstraint(SpringLayout.NORTH, comboBox_3, 0,
				SpringLayout.NORTH, lblBirthdate);
		add(comboBox_3);

		insurance = new JTextField();
		springLayout.putConstraint(SpringLayout.WEST, insurance, 0,
				SpringLayout.WEST, typeBox);
		add(insurance);
		insurance.setColumns(10);

		JLabel lblInsurance = new JLabel("Insurance");
		lblInsurance.setForeground(Color.WHITE);
		springLayout.putConstraint(SpringLayout.NORTH, insurance, -2,
				SpringLayout.NORTH, lblInsurance);
		springLayout.putConstraint(SpringLayout.NORTH, lblInsurance, 30,
				SpringLayout.SOUTH, lblBirthdate);
		springLayout.putConstraint(SpringLayout.WEST, lblInsurance, 0,
				SpringLayout.WEST, lblUserType);
		add(lblInsurance);

		pharmacy = new JTextField();
		springLayout.putConstraint(SpringLayout.WEST, pharmacy, 0,
				SpringLayout.WEST, typeBox);
		add(pharmacy);
		pharmacy.setColumns(10);

		JLabel lblPharmacy = new JLabel("Pharmacy");
		lblPharmacy.setForeground(Color.WHITE);
		springLayout.putConstraint(SpringLayout.NORTH, pharmacy, -2,
				SpringLayout.NORTH, lblPharmacy);
		springLayout.putConstraint(SpringLayout.NORTH, lblPharmacy, 30,
				SpringLayout.SOUTH, lblInsurance);
		springLayout.putConstraint(SpringLayout.WEST, lblPharmacy, 0,
				SpringLayout.WEST, lblUserType);
		add(lblPharmacy);

		JLabel lblCreateUser = new JLabel("Create User");
		lblCreateUser.setForeground(Color.WHITE);
		springLayout.putConstraint(SpringLayout.NORTH, lblCreateUser, 70,
				SpringLayout.NORTH, this);
		lblCreateUser.setHorizontalAlignment(SwingConstants.CENTER);
		springLayout.putConstraint(SpringLayout.WEST, lblCreateUser, 0,
				SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST, lblCreateUser, 0,
				SpringLayout.EAST, this);
		lblCreateUser.setFont(new Font("Dialog", Font.BOLD, 20));
		add(lblCreateUser);

		name = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, name, -2,
				SpringLayout.NORTH, lblName);
		springLayout.putConstraint(SpringLayout.WEST, name, 0,
				SpringLayout.WEST, typeBox);
		add(name);
		name.setColumns(10);

		JButton btnCreate = new JButton("Create");
		springLayout.putConstraint(SpringLayout.WEST, btnCreate, 350, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.SOUTH, btnCreate, -35, SpringLayout.SOUTH, this);
		springLayout.putConstraint(SpringLayout.EAST, btnCreate, -350,
				SpringLayout.EAST, this);
		btnCreate.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				String[] s = new String[] {
						((UserType) typeBox.getSelectedItem()).toString(),
						name.getText(),
						(rdbtnM.isSelected()) ? "M" : "F",
						city.getText(),
						region.getText(),
						"" + comboBox_1.getSelectedItem(),
						"" + comboBox_2.getSelectedItem(),
						"" + comboBox_3.getSelectedItem(),
						insurance.getText(),
						pharmacy.getText(),
						phone.getText()};

				if (CommandBuilder.validCreateUser(s)) {
					Command com = CommandBuilder.createUser(s);
					session.client.put(com);
					
					// Reset everything
					typeBox.setSelectedIndex(0);
					
					if (rdbtnM.isSelected()) {
						rdbtnM.setSelected(false);
					}
					
					if (rdbtnF.isSelected()) {
						rdbtnF.setSelected(false);
					}
					
					name.setText("");
					city.setText("");
					region.setText("");
					insurance.setText("");
					pharmacy.setText("");
					phone.setText("");
					
					comboBox_1.setSelectedIndex(0);
					comboBox_2.setSelectedIndex(0);
					comboBox_3.setSelectedIndex(0);
				} else {
					// TODO Pop up an error
				}
			}
		});
		add(btnCreate);

		JLabel lblPhone = new JLabel("Phone");
		lblPhone.setForeground(Color.WHITE);
		springLayout.putConstraint(SpringLayout.NORTH, lblPhone, 30, SpringLayout.SOUTH, lblPharmacy);
		springLayout.putConstraint(SpringLayout.WEST, lblPhone, 0, SpringLayout.WEST, lblUserType);
		add(lblPhone);
		
		phone = new JTextField();
		springLayout.putConstraint(SpringLayout.WEST, phone, 0, SpringLayout.WEST, typeBox);
		springLayout.putConstraint(SpringLayout.SOUTH, phone, 0, SpringLayout.SOUTH, lblPhone);
		add(phone);
		phone.setColumns(10);
		
		ImagePanel panel = new ImagePanel(Client.back);
		add(panel);

		Dimension size = new Dimension(Client.WIDTH, Client.HEIGHT);
		setPreferredSize(size);
		setMinimumSize(size);
		setMaximumSize(size);
		setSize(size);
	}
}
